package cn.kgc.controller;

import cn.kgc.base.dto.UmsMemberDTO;
import cn.kgc.base.dto.UmsRoleDTO;
import cn.kgc.commons.vo.Result;
import cn.kgc.entity.UmsMemberRole;
import cn.kgc.entity.UmsMemeberRole;
import cn.kgc.entity.UmsRole;
import cn.kgc.service.UmsMemberRoleService;
import cn.kgc.service.UmsRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>添加描述信息</p>
 *
 * @author 卢传琪
 */
@RestController
@RequestMapping("/ums/role")
@Api(tags = "角色")
@Validated
public class RoleController {
    @Resource
    private UmsMemberRoleService umsMemberRoleService;

    @Resource
    private UmsRoleService umsRoleService;

    @GetMapping("/getRole")
    @ApiOperation(value = "根据用户ID查询角色信息", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @CrossOrigin
    @ApiImplicitParam(name = "memberId",value = "会员id",dataType = "long",paramType = "query")
    public List<UmsRoleDTO> getUmsRole(Long memberId){
        List<UmsRoleDTO> umsRoles=new ArrayList<>();
        LambdaQueryWrapper<UmsMemberRole> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UmsMemberRole::getMemberId,memberId);
        //用户对应的角色的id
        List<UmsMemberRole> umsMemberRoleList=umsMemberRoleService.list(lambdaQueryWrapper);
        UmsRoleDTO umsRoleDTO=new UmsRoleDTO();
        for (UmsMemberRole umsMemberRole : umsMemberRoleList) {
            long roleId=umsMemberRole.getRoleId();
            UmsRole umsRole=umsRoleService.getById(roleId);
            BeanUtils.copyProperties(umsRole,umsRoleDTO);
            umsRoles.add(umsRoleDTO);
        }
        return umsRoles;
    }
}
